package com.example.allplan.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.allplan.Fragment.JiHuaFragment;
import com.example.allplan.JavaBean.JiHuaBaoJavaBean;
import com.example.allplan.MyApplication;
import com.example.allplan.R;
import com.example.allplan.RecyclerAdapter.BiaoQingLieBiaoAdapter;
import com.example.allplan.StaticFangfa.BiaoQingZhuanHuan;

public class BianJiYiGeJiHuaBao extends BaseActivity {
    Toolbar biao_ti_toolbar;
    EditText mshu_ru_edittext;
    TextView biao_qing_textview;
    RecyclerView biao_qing_lie_biao;
    BiaoQingLieBiaoAdapter mbiao_Qing_Lie_Biao_Adapter;
    JiHuaBaoJavaBean mresult;
    MyApplication mmyapp;
    Boolean isnew;
    int pos_zhi;
    final int TIAN_JIA_HUO_GENG_XIN_JI_HUA_BAO_CODE=11,TIAN_JIA_HUO_GENG_XIN_JI_HUA_BAO_RESULT_CODE=12;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bian_ji_yi_ge_ji_hua_bao_activity);
        init();

    }

    void init() {
        mmyapp=MyApplication.getInstance();
        mresult=new JiHuaBaoJavaBean();
        mshu_ru_edittext=findViewById(R.id.shu_ru_bao_ming_edittext);
        biao_ti_toolbar = findViewById(R.id.bian_ji_yi_ge_ji_hua_bao_toolbar);
        setSupportActionBar(biao_ti_toolbar);
        biao_qing_textview=findViewById(R.id.bian_ji_yi_ge_ji_hua_bao_biao_qing_textview);
        if(getIntent().getExtras()!=null){
            Bundle mb=getIntent().getExtras();
            mresult.setBiao_qing_fu_hao_zhi(mb.getInt("biao_qing_zhi",128516));
            mresult.setId(mb.getInt("id",-2));
            mresult.setBao_ming(mb.getString("biao_ti_ming",""));
            mresult.setPai_xu_you_biao(mb.getInt("pai_xu_you_biao"));
            mresult.setLei_xing(mb.getInt("lei_xing"));
            mresult.setJi_hua_shu(mb.getInt("ji_hua_shu"));
            pos_zhi=mb.getInt("pos_zhi");
            mshu_ru_edittext.setText(mresult.getBao_ming());
            isnew=false;
        }else{
            mresult.setBiao_qing_fu_hao_zhi(128516);
            mresult.setId(mmyapp.ji_hua_bao_xia_biao);
            mresult.setBao_ming("");
            mresult.setJi_hua_shu(0);
            mresult.setLei_xing(1);
            mresult.setPai_xu_you_biao(mmyapp.ji_hua_bao_shu);
            isnew=true;
        }

        biao_qing_textview.setText(BiaoQingZhuanHuan.biao_qing_int_to_string(mresult.getBiao_qing_fu_hao_zhi()));
        biao_qing_lie_biao=findViewById(R.id.biao_qing_lie_biao_recyclerview);

        mbiao_Qing_Lie_Biao_Adapter=new BiaoQingLieBiaoAdapter(mmyapp.mBiao_Qing_int_Array, this, new BiaoQingLieBiaoAdapter.onItemClickListener() {
            @Override
            public void onClick(int pos) {
                biao_qing_textview.setText(mmyapp.mBiao_Qing_int_Array.get(pos).getBiao_qing_string());
                mresult.setBiao_qing_fu_hao_zhi(mmyapp.mBiao_Qing_int_Array.get(pos).getKaishi());
            }
        });
        biao_qing_lie_biao.setAdapter(mbiao_Qing_Lie_Biao_Adapter);
        biao_qing_lie_biao.setLayoutManager(new GridLayoutManager(this,6));
    }
    boolean check(){
        if(mshu_ru_edittext.getText().toString().equals("")){
            return false;
        }else{
            mresult.setBao_ming(mshu_ru_edittext.getText().toString());
            return true;
        }
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.bian_ji_yi_ge_ji_hua_bao_wan_cheng:
                if(!check()){
                    Toast.makeText(this, "请输入标题", Toast.LENGTH_SHORT).show();
                }else{
                    if(isnew){
                        mmyapp.mSuoYouJiHuaBaoSQCaoZuo.write_one(mresult);
                        mmyapp.ji_hua_bao_shu++;
                        mmyapp.putSH_Int(mmyapp.JI_HUA_BAO_SHU_SHARE_NAME,mmyapp.ji_hua_bao_shu);
                        mmyapp.ji_hua_bao_xia_biao++;
                        mmyapp.putSH_Int(mmyapp.JI_HUA_BAO_XIA_BIAO_SHARE_NAME,mmyapp.ji_hua_bao_xia_biao);
                    }else{
                        mmyapp.mSuoYouJiHuaBaoSQCaoZuo.update_one_only_biao_qing_and_bao_ming(mresult);
                    }
                    Intent mintent=new Intent();
                    mintent.putExtra("pos_zhi",pos_zhi);
                    mintent.putExtra("geng_gai_de_id",mresult.getId());
                    setResult(TIAN_JIA_HUO_GENG_XIN_JI_HUA_BAO_RESULT_CODE,mintent);
                    finish();
                }
                break;
            case android.R.id.home:
                finish();
                break;
        }
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.bian_ji_yi_ge_ji_hua_bao_menu,menu);
    return true;
    }
}
